package com.datang.petstore.service.impl;


import com.datang.petstore.entity.Microblog;
import com.datang.petstore.entity.MicroblogInformRecord;
import com.datang.petstore.entity.MicroblogInformRecordExample;
import com.datang.petstore.entity.Role;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.generatemapper.MicroblogInformRecordMapper;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.MicroblogInformRecordService;
import com.datang.petstore.service.MicroblogService;
import com.datang.petstore.service.RoleService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-03 15:00
 * @notify 举报记录
 * @version 1.0
 */
@Service
public class MicroblogInformRecordServiceImpl implements MicroblogInformRecordService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MicroblogService microblogService;

    @Autowired
    private MicroblogInformRecordMapper microblogInformRecordMapper;

    /*
     * @author 顶风少年
     * @date 2019/11/3 15:02
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @param microblogId
     * @result void
     * @notify 添加举报记录
     * version 1.0
     */
    @Transactional
    public void addMicroblogInformRecord(MicroblogInformRecord microblogInformRecord) {
        //查询原微博
        Microblog microblog1 = microblogService.getMicroblog(microblogInformRecord.getMicroblogId());
        if (microblog1.getDel().equals("1")) {
            throw new AcceptResultCodeException(ResultCode.error505);
        }

        //查询用户是否举报过该微博
        List<MicroblogInformRecord> microblogInformRecordByUserId = getMicroblogInformRecord(microblogInformRecord.getUserId(), microblogInformRecord.getMicroblogId());
        if (microblogInformRecordByUserId.size() > 0) {
            throw new AcceptResultCodeException(ResultCode.error507);
        }

        //查询执行举报的用户有没有直接删除微博的权限
        List<Role> roleByUserId = roleService.getRoleByUserId(microblogInformRecord.getUserId());
        Stream<Role> user_manager = roleByUserId.stream().filter(r -> r.getName().equals("user_manager"));
        List<Role> collect = user_manager.collect(Collectors.toList());

        Microblog microblog = new Microblog();
        //设置微博id
        microblog.setId(microblogInformRecord.getMicroblogId());
        //设置举报次数
        microblog.setInformNumber(microblog1.getInformNumber() + 1);
        //如果是管理员则直接删除
        if (collect.size() != 0) {
            //设置删除
            microblog.setDel("1");
        }
        //更改微博状态
        microblogService.updateMicroblog(microblog);

        //添加举报记录
        microblogInformRecord.setDate(new Date());
        microblogInformRecord.setMicroblogUserId(microblog1.getUserId());
        microblogInformRecordMapper.insertSelective(microblogInformRecord);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/3 16:47
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result java.util.List<com.datang.petstore.entity.MicroblogInformRecord>
     * @notify 根据用户id和微博id查询举报记录
     * version 1.0
     */
    public List<MicroblogInformRecord> getMicroblogInformRecord(String userId, Integer microblogId) {
        MicroblogInformRecordExample microblogInformRecordExample = new MicroblogInformRecordExample();
        MicroblogInformRecordExample.Criteria criteria = microblogInformRecordExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andMicroblogIdEqualTo(microblogId);
        List<MicroblogInformRecord> microblogInformRecords = microblogInformRecordMapper.selectByExample(microblogInformRecordExample);
        return microblogInformRecords;
    }
}
